package com.yykj.im.center.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yykj.common.util.LocalStringUtil;
import com.yykj.im.center.client.dto.ClientSystemNoticeDto;
import com.yykj.im.center.client.dto.SystemNoticeDto;
import com.yykj.im.center.client.dto.base.PaginationListDto;
import com.yykj.im.center.client.form.base.BasePaginationForm;
import com.yykj.im.center.client.form.consultation.*;
import com.yykj.im.center.server.entity.Notice;
import com.yykj.im.center.server.exception.ErrorCode;
import com.yykj.im.center.server.exception.ErrorCodeException;
import com.yykj.im.center.server.repository.NoticeMapper;
import com.yykj.im.center.server.service.BizService;
import com.yykj.im.center.server.util.ConvertUtil;
import com.yykj.im.center.server.util.LocalDateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
@Transactional
public class BizServiceImpl implements BizService {

    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public void systemNoticeCondition(SystemNoticeConditionForm systemNoticeConditionForm) {
        List<UserInfoForm> users = systemNoticeConditionForm.getUsers();
        if (users == null) {//TODO 如果前端不传 自己根据条件把符合条件的用户捞出来
        }
        Notice notice = new Notice();
        BeanUtils.copyProperties(systemNoticeConditionForm, notice);
        String sendTime = systemNoticeConditionForm.getSendTime();
        LocalDateTime now = LocalDateTimeUtil.now();
        LocalDateTime sendT = LocalDateTimeUtil.now();
        if(StringUtils.isNotBlank(sendTime)){
            sendT = LocalDateTimeUtil.second2LocalDateTime(sendTime);
        }
        StringBuilder sb = new StringBuilder();
        for (UserInfoForm user : users) {
            sb.append(user.getTencentAccountId()).append(",").append(user.getName()).append("@@@");
        }
        notice.setUsers(sb.substring(0, sb.length() - 3).toString());
        notice.setId(LocalStringUtil.getUUID());
        notice.setCreateTime(now);
        notice.setSendTime(sendT);
        this.noticeMapper.insert(notice);
    }

    @Override
    public PaginationListDto<SystemNoticeDto> systemNoticeList(SystemNoticeListForm systemNoticeListForm) {
        PageHelper.startPage(systemNoticeListForm.getPageNum(), systemNoticeListForm.getPageSize());
        Page<Notice> notices = this.noticeMapper.listByCondition(systemNoticeListForm);
        List<SystemNoticeDto> systemNoticeDtos = null;
        if (notices != null && notices.size() > 0) {
            systemNoticeDtos = new ArrayList<>();
            for (Notice notice : notices) {
                SystemNoticeDto systemNoticeDto = ConvertUtil.convert(notice, SystemNoticeDto.class);
                systemNoticeDto.setUsers(ConvertUtil.getUserInfo(notice.getUsers()));
                systemNoticeDtos.add(systemNoticeDto);
            }
        }
        PaginationListDto<SystemNoticeDto> systemNoticeDto = new PaginationListDto<>();
        systemNoticeDto.setList(systemNoticeDtos);
        systemNoticeDto.setPageNum(systemNoticeListForm.getPageNum());
        systemNoticeDto.setPageSize(systemNoticeListForm.getPageSize());
        systemNoticeDto.setTotal(notices.getTotal());
        return systemNoticeDto;
    }

    @Override
    public void systemNoticeUpdate(SystemNoticeUpdateForm systemNoticeUpdateForm) {
        Notice notice = this.noticeMapper.findById(systemNoticeUpdateForm.getId());
        if (notice.getStatus() == 1) {
            throw new ErrorCodeException(ErrorCode._9034);
        }
        Notice noticeNew = ConvertUtil.convert(notice, Notice.class);
        this.noticeMapper.updateById(notice);
        noticeNew.setId(LocalStringUtil.getUUID());
        if (StringUtils.isNotBlank(systemNoticeUpdateForm.getTitle())) {
            noticeNew.setTitle(systemNoticeUpdateForm.getTitle());
        }
        if (systemNoticeUpdateForm.getGender() != null) {
            noticeNew.setGender(systemNoticeUpdateForm.getGender());
        }
        if (systemNoticeUpdateForm.getDepartmentId() != null) {
            noticeNew.setDepartmentId(systemNoticeUpdateForm.getDepartmentId());
        }
        if (systemNoticeUpdateForm.getHospitalId() != null) {
            noticeNew.setHospitalId(systemNoticeUpdateForm.getHospitalId());
        }
        if (StringUtils.isNotBlank(systemNoticeUpdateForm.getSendTime())) {
            noticeNew.setSendTime(LocalDateTimeUtil.second2LocalDateTime(systemNoticeUpdateForm.getSendTime()));
        }
        if (systemNoticeUpdateForm.getUsers() != null) {
            StringBuilder sb = new StringBuilder();
            for (UserInfoForm user : systemNoticeUpdateForm.getUsers()) {
                sb.append(user.getTencentAccountId()).append(",").append(user.getName()).append("@@@");
            }
            notice.setUsers(sb.substring(0, sb.length() - 3).toString());
        }
        this.noticeMapper.insert(noticeNew);
    }

    @Override
    public void systemNoticeSendAgain(SystemNoticeSendAgainForm systemNoticeSendAgainForm) {
        Notice notice = this.noticeMapper.findById(systemNoticeSendAgainForm.getId());
        Notice noticeNew = ConvertUtil.convert(notice, Notice.class);
        noticeNew.setId(LocalStringUtil.getUUID());
        LocalDateTime now = LocalDateTimeUtil.now();
        LocalDateTime sendTime = LocalDateTimeUtil.now();
        if (StringUtils.isNotBlank(systemNoticeSendAgainForm.getSendTime())) {
            sendTime = LocalDateTimeUtil.second2LocalDateTime(systemNoticeSendAgainForm.getSendTime());
        }
        noticeNew.setSendTime(sendTime);
        noticeNew.setCreateTime(now);
        noticeNew.setDele(0);
        noticeNew.setStatus(0);
        noticeNew.setMsgKey(null);
        noticeNew.setDoSendTime(null);
        this.noticeMapper.insert(noticeNew);
    }

    @Override
    public void systemNoticeDelete(String id) {
        Notice notice = this.noticeMapper.findById(id);
        notice.setDele(1);
        this.noticeMapper.updateById(notice);
    }

    @Override
    public PaginationListDto<ClientSystemNoticeDto> clientSystemNoticeList(BasePaginationForm basePaginationForm) {
        PageHelper.startPage(basePaginationForm.getPageNum(), basePaginationForm.getPageSize());
        Page<Notice> notices = this.noticeMapper.listByStatus(1);
        List<ClientSystemNoticeDto> clientSystemNoticeDtos = ConvertUtil.convertList(notices, ClientSystemNoticeDto.class);
        PaginationListDto<ClientSystemNoticeDto> systemNoticeDto = new PaginationListDto<>();
        systemNoticeDto.setList(clientSystemNoticeDtos);
        systemNoticeDto.setPageNum(basePaginationForm.getPageNum());
        systemNoticeDto.setPageSize(basePaginationForm.getPageSize());
        systemNoticeDto.setTotal(notices.getTotal());
        return systemNoticeDto;
    }
}